﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCRUM.Core.BusinessObjects
{
	/// <summary>
	/// Business object for release.
	/// </summary>
    [Serializable()]
    public class Release : HierarchicalBusinessObject
    {
        /// <summary>
        /// Gets or sets the superior item's ID.
        /// </summary>
        /// <value>The superior item ID.</value>
        public override int SuperiorItemID 
        {
            get { return this.ProjectID; }
            set { this.ProjectID = value; }
        }

        /// <summary>
        /// Gets or sets the version.
        /// </summary>
        /// <value>The version.</value>
        public string Version { get; set; }

        /// <summary>
        /// Gets or sets the project ID.
        /// </summary>
        /// <value>The project ID.</value>
        public int ProjectID { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Release"/> class.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="name">The name of the release.</param>
        /// <param name="version">The version.</param>
        public Release(int id, string name, string version, double estimation, double progress, int projectID, DateTime change)
            :base(id,name,estimation,progress,change)
        {
            this.Version = version;
            this.ProjectID = projectID;
        }

        /// <summary>
        /// Gets or sets the sprints.
        /// </summary>
        /// <value>The sprints.</value>
        public IList<Sprint> Sprints
        {
            get
            {
                // Lazy loading
                if (this.sprints == null)
                {
                    this.sprints = DAO.DAOSprint.GetAllSprintsForRelease(this.ID);
                }
                return this.sprints;
            }
            set
            {
                this.sprints = value;
            }
        }

        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>The project.</value>
        public Project Project 
        { 
            get 
            {
                // Lazy loading
                if (this.project == null)
                {
                    this.project = DAO.DAOProject.GetProjectByReleaseID(this.ID);
                }
                return this.project;
            }
            set
            {
                this.project = value;
            }
        }

        /// <summary>
        /// List of sprints.
        /// </summary>
        private IList<Sprint> sprints;

        /// <summary>
        /// Project that contains this release.
        /// </summary>
        private Project project;
                
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            Release rel = obj as Release;
            return this.ID.Equals(rel.ID) &&
                   this.Name.Equals(rel.Name) &&
                   this.Version.Equals(rel.Version);
        }

		/// <summary>
		/// Returns a hash code for this instance.
		/// </summary>
		/// <returns>
		/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
		/// </returns>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
    }
}
